package com.postgis;

import org.postgresql.jdbc3.Jdbc3PoolingDataSource;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JavaGIS {
    public static void main(String[] args) throws SQLException {

        Jdbc3PoolingDataSource ds = new Jdbc3PoolingDataSource();

        ds.setServerName("121.41.18.253");
        ds.setPortNumber(5432);
        ds.setDatabaseName("customer");
        ds.setUser("luhuijun");
        ds.setPassword("Abc12345");
        ds.setInitialConnections(3);
        ds.setMaxConnections(20);


        Connection conn = ds.getConnection();

        try {

            Statement s = conn.createStatement();
            ResultSet r = s.executeQuery("select *,ST_Distance(jwd, ST_Transform(ST_GeomFromText('POINT(121.41011 31.17185)', 4326), 2163)) " +
                    "from building_gps_info order by jwd <-> ST_Transform(ST_GeomFromText('POINT(121.41011 31.17185)', 4326), 2163) limit 10;");
            while (r.next()) {

                Object geom = r.getObject(2);
                int id = r.getInt(2);
                System.out.println("Row " + id + ":");
                System.out.println(geom.toString());
            }
            s.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}